<template>
    <u-popup ref="popupPrivacy" type="center" :show="innerShow">
        <view class="contents">
            <view class="Mask-cnontent">
                <view class="title">用户隐私协议提示</view>
                <text class="text">
                    感谢您使用十蜂健康小程序，您在使用小程序前请应当阅读并同意
                </text>
                <text class="privacyText" @click="goRule">
                    {{ privacyContractName }}
                </text>
                <text class="text">
                    如你同意该指引，请点击“同意”开始使用本小程序。
                </text>
                <view class="btn-box flex_wrap">
                    <view class="btn-tow" @click="disagree()">拒绝</view>
                    <!-- 微信小程序隐私授权按钮，open-type仅小程序支持 -->
                    <button
                        v-if="isWeChat"
                        class="btn-one"
                        id="agree-btn"
                        type="default"
                        open-type="agreePrivacyAuthorization"
                        @agreeprivacyauthorization="handleAgree"
                    >
                        同意
                    </button>
                    <view v-else class="btn-one" @click="handleAgree">
                        同意
                    </view>
                </view>
            </view>
        </view>
    </u-popup>
</template>

<script>
export default {
    name: "privacySet",
    data() {
        return {
            innerShow: false,
            privacyContractName: "《十蜂健康小程序隐私政策》",
            isWeChat: false,
        };
    },
    computed: {
        isWeChat() {
            // 检查是否为微信小程序环境
            return typeof wx !== 'undefined' && wx.getPrivacySetting;
        }
    },
    // 组件创建即执行
    created() {
        //可以视情况关闭
        // this.getPrivacySet();
    },
    methods: {
        //查看协议
        goRule: function (e) {
            // 微信小程序隐私协议接口，APP端使用uni.showModal提示
            if (typeof wx !== 'undefined' && wx.openPrivacyContract) {
                wx.openPrivacyContract({
                    success: (res) => {
                        console.log("隐式协议成功");
                    },
                    fail: (res) => {
                        console.error("隐式协议失败", res);
                    },
                });
            } else {
                uni.showModal({
                    title: "提示",
                    content: "请阅读隐私政策后继续使用",
                    showCancel: false
                });
            }
        },
        // 同意
        handleAgree() {
            this.innerShow = false;
            this.$emit("initData", true);
        },
        //不同意协议
        disagree() {
            let _this = this;
            uni.showModal({
                title: "提示",
                content:
                    "拒绝后，可能会导无法登录及其他功能使用，你确定要拒绝吗？",
                confirmText: "确定",
                cancelText: "取消",
                success: (res) => {
                    if (res.confirm) {
                        _this.innerShow = false;
                        //同意协议后本组件调用父组件方法
                        _this.$emit("initData", false);
                    }
                },
            });

            // uni.navigateBack() //可以视情况打开
        },

        // 检测用户是否需要开启权限
        getPrivacySet() {
            let _this = this;
            // 微信小程序隐私设置接口，仅小程序支持
            if (typeof wx !== 'undefined' && wx.getPrivacySetting) {
                wx.getPrivacySetting({
                    success: (res) => {
                        console.log("组件检测授权状况", res);
                        if (res.needAuthorization) {
                            uni.hideLoading();
                            // 需要弹出隐私协议
                            _this.$nextTick(function () {
                                _this.innerShow = true;
                            });
                            _this.privacyContractName = res.privacyContractName;
                        } else {
                            // 用户已经同意过隐私协议
                            _this.privacyContractName = res.privacyContractName;
                            _this.$emit("initSetting", true);
                        }
                    },
                });
            } else {
                // APP或低版本，直接认为已同意
                console.log('非小程序环境或低版本，跳过隐私设置检测')
                _this.$emit("initSetting", true);
            }
        },
    },
};
</script>

<style lang="scss">
.contents {
    width: 100%;
    min-height: 680rpx;
    // background-color: #ffffff;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20rpx 0;

    // display: ;
    .Mask-cnontent {
        // width: 90%;
        padding: 30rpx;
        background-color: #ffffff;
        border-radius: 16rpx;
        display: flex;

        flex-direction: column;
        // justify-content: space-between;

        .title {
            font-size: 40rpx;
            margin-bottom: 10rpx;
        }

        .privacyText {
            color: #588bf3;
            display: inline-block;
            font-weight: 500;
        }

        .text {
            margin: 20rpx 0;
            color: #777777;
            font-size: 32rpx;
            display: inline-block;
        }

        .btn-box {
            align-items: center;
            padding-left: 34rpx;
            .btn-one {
                width: 278rpx;
                height: 92rpx;
                background: #588af1;
                border-radius: 16rpx 16rpx 16rpx 16rpx;
                font-size: 32rpx;
                line-height: 92rpx;
                letter-spacing: 6rpx;
                text-align: center;
                color: #fff;
            }

            .btn-tow {
                width: 278rpx;
                height: 92rpx;
                background: #f2f2f2;
                border-radius: 16rpx 16rpx 16rpx 16rpx;
                font-size: 32rpx;
                line-height: 92rpx;
                letter-spacing: 6rpx;
                text-align: center;
                color: #588af1;
            }
        }
    }
}
</style>
